package cn.lccabc.attend.service;

import cn.lccabc.attend.dao.AttendMapper;
import cn.lccabc.attend.entity.Attend;
import cn.lccabc.attend.vo.QueryCondition;
import cn.lccabc.common.page.PageQueryBean;
import cn.lccabc.common.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by L on 2018/4/7.
 */
@Service(value = "attendServiceImpl")
public class AttendServiceImpl implements AttendService {

    /**
     * 中午十二点，判定上下午
     */
    private static final int NOON_HOUR = 12;
    private static final int NOON_MINUTE = 00;

    /**
     * 缺勤一天
     */
    private static final Integer ABSENCE_DAY = 480;

    /**
     * 考勤异常状态
     */
    private static final Byte ATTEND_STATUS_ABNORMAL = 2;
    private static final Byte ATTEND_STATUS_NORMAL = 1;

    private Log log = LogFactory.getLog(AttendServiceImpl.class);

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");


    @Autowired
    private AttendMapper attendMapper;

    @Override
    @Transactional
    public void signAttend(Attend attend) throws Exception {
        Date today = new Date();
//        Date today = dateFormat.parse("2018-04-19 21:13:00");
        Date noon = DateUtils.getDate(NOON_HOUR, NOON_MINUTE);
        attend.setAttend_date(today);
        attend.setAttend_week((byte) DateUtils.getTodayWeek());
        Attend todayRecord = attendMapper.selectTodaySignRecord(attend.getUser_id());
        try {
            if (todayRecord==null){
                //打卡记录不存在
                if (today.compareTo(noon)<=0) {
                    //打卡时间早于十二点，上午打卡
                    attend.setAttend_morning(today);
                } else {
                    attend.setAttend_evening(today);
                }
                attendMapper.insertSelective(attend);
            } else {
                //数据存在
                if (today.compareTo(noon)<=0) {
                    //打卡时间早于十二点，上午打卡
                    return;
                } else {
                    todayRecord.setAttend_evening(today);
                    attendMapper.updateByPrimaryKeySelective(todayRecord);
                }
            }
        } catch (Exception e){
            log.error("用户签到异常",e);
            throw e;
        }
    }

    @Override
    public PageQueryBean listAttend(QueryCondition condition) {
        //根据查询条件，查询数据数目
        int count = attendMapper.countByCondition(condition);
        List<Attend> attendList = null;
        PageQueryBean pageResult = new PageQueryBean();
        if (count>0) {
            pageResult.setTotalRows(count);
            pageResult.setCurrentPage(condition.getCurrentPage());
            pageResult.setPageSize(condition.getPageSize());
            attendList = attendMapper.selectAttendPage(condition);
            pageResult.setItems(attendList);
        }
        return pageResult;
    }

    @Override
    public void checkAttend() {
        List<Long> userIds = attendMapper.selectTodayAbsence();
        if (CollectionUtils.isNotEmpty(userIds)){
            List<Attend> attends = new ArrayList<Attend>();
            for (Long userId : userIds){
                Attend attend = new Attend();
                attend.setUser_id(userId);
                attend.setAttend_date(new Date());
                attend.setAttend_week((byte)DateUtils.getTodayWeek());
                attend.setAbsence(ABSENCE_DAY);
                attend.setAttend_status(ATTEND_STATUS_ABNORMAL);
                attends.add(attend);
            }
            attendMapper.batchInsert(attends);
        }
    }
}
